package com.sjd.web.controller.farm;

import com.sjd.common.core.controller.BaseController;
import com.sjd.common.core.domain.AjaxResult;
import com.sjd.common.core.page.TableDataInfo;
import com.sjd.common.utils.poi.ExcelUtil;
import com.sjd.farm.domain.dto.order.WebFarmOrderQryRequestDTO;
import com.sjd.farm.domain.dto.order.WebFarmOrderQryResponseDTO;
import com.sjd.farm.service.WebFarmOrderService;
import com.sjd.mall.domain.dto.base.OrderNoRequestDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * WebFarmOrder
 *
 * @author seer
 * @date 2022/2/5 13:30
 */
@Api(tags = "农场订单模块(管理平台)")
@RestController
@RequestMapping(value = "/farm/order")
@Slf4j
public class WebFarmOrderController extends BaseController {

    @Autowired
    private WebFarmOrderService orderService;

    /**
     * 农场订单确认收货
     *
     * @param requestDTO requestDTO
     * @return object
     */
    @ApiOperation(value = "确认收货")
    @PreAuthorize("@ss.hasAnyPermi('farm:order:receipt')")
    @PostMapping("/receipt")
    public AjaxResult receipt(@Validated @RequestBody OrderNoRequestDTO requestDTO) {
        log.info("农场订单确认收货,请求 {}", requestDTO);
        AjaxResult result = orderService.receipt(requestDTO);
        log.info("农场订单确认收货,响应 {}", result);
        return result;
    }

    /**
     * 农场订单查询
     *
     * @param requestDTO requestDTO
     * @return object
     */
    @ApiOperation(value = "订单查询")
    @PreAuthorize("@ss.hasAnyPermi('farm:order:qry')")
    @GetMapping("/qry")
    public TableDataInfo qry(WebFarmOrderQryRequestDTO requestDTO) {
        startPage();
        if (log.isDebugEnabled()) {
            log.debug("农场订单查询,请求 {}", requestDTO);
        }
        List<WebFarmOrderQryResponseDTO> respList = orderService.qry(requestDTO);
        if (log.isDebugEnabled()) {
            log.debug("农场订单查询,响应 {}", respList);
        }
        return getDataTable(respList);
    }

    /**
     * 农场订单导出
     *
     * @param requestDTO requestDTO
     */
    @ApiOperation(value = "订单导出")
    @PreAuthorize("@ss.hasAnyPermi('farm:order:export')")
    @PostMapping("/export")
    public void export(WebFarmOrderQryRequestDTO requestDTO, HttpServletResponse response) {
        if (log.isDebugEnabled()) {
            log.debug("农场订单导出,请求 {}", requestDTO);
        }
        List<WebFarmOrderQryResponseDTO> respList = orderService.qry(requestDTO);
        ExcelUtil<WebFarmOrderQryResponseDTO> util = new ExcelUtil<>(WebFarmOrderQryResponseDTO.class);
        util.exportExcel(response, respList, "农场订单信息");
    }
}
